$(function() {
	// Dialog strutture opache
	$('#X-browse-constructions').dialog({
		width : 850,
		minHeight : 528,
		maxHeight : 528,
		modal : true,
		resizable : false,
		draggable : false,
		autoOpen : false,
		buttons : {
			Chiudi : function() {
				$(this).dialog('close');
			}
		}
	});

	// Dialog strutture trasparenti
	$('#X-browse-glazings').dialog({
		width : 850,
		minHeight : 528,
		maxHeight : 528,
		modal : true,
		resizable : false,
		draggable : false,
		autoOpen : false,
		buttons : {
			Chiudi : function() {
				$(this).dialog('close');
			}
		}
	});
	
	// Triggers selezione pareti, porte e pavimenti
	$('#wall_north, #wall_east, #wall_south, #wall_west, #door_north, #door_east, #door_south, #door_west, #floor, #ceiling').click(function(ev) {
		construction_dialog_originator.name = $(ev.target);
		construction_dialog_originator.id = $('#' + ev.target.id + "_id");
		$('#X-browse-constructions').dialog('open');
	});

	// Triggers selezione finestre
	$('#window_north, #window_east, #window_south, #window_west').click(function(ev) {
		construction_dialog_originator.name = $(ev.target);
		construction_dialog_originator.id = $('#' + ev.target.id + "_id");
		$('#X-browse-glazings').dialog('open');
	});

	// Triggers e Listeners descrizione status pareti
	$('[name^="wall"]').change(function() {
		$(document).trigger('wall-status-change', [this.name, this.value]); 
	});
	$(document).bind('wall-status-change', function(ev, wall, status) {
		setWallPanelHints(wall, status);
		setDoorAndWindowPanelVisibility(wall, status);
	});

	// Listeners descrizione status pavimenti/soffitti
	$('#geometry_storey').change(function() {
		$("#floor + .hint").text(getHintForFloor(this.value));
		$("#ceiling + .hint").text(getHintForCeiling(this.value));
	});

	/*
	 * Setup dataTables (lista categorie nelle dialogs)
	 */
	var cTable = $('#dt_constructions').dataTable({
		"oLanguage" : {
			"sUrl" : "/js/dataTables/jquery.dataTables.it_IT.txt"
		},
		"bJQueryUI" : true,
		//"sDom" : '<"H"lfr>t<"F"ip>',
		"iDisplayLength" : 10,
		"sPaginationType" : "full_numbers",
		"bStateSave" : false,
		"bAutoWidth" : false,
		"bInfo": false,
		"aoColumns" : [ { "sWidth": "600px" }, { "sWidth": "250px" } ],
	});
	$("tfoot select").change(function() {
		/* Filter on the column (the index) of this element */
		cTable.fnFilter("" + this.value, 1);
	});
	
	var gTable = $('#dt_glazings').dataTable({
		"oLanguage" : {
			"sUrl" : "/js/dataTables/jquery.dataTables.it_IT.txt"
		},
		"bJQueryUI" : true,
		//"sDom" : '<"H"lfr>t<"F"ip>',
		"iDisplayLength" : 10,
		"sPaginationType" : "full_numbers",
		"bStateSave" : false,
		"bAutoWidth" : false,
		"bInfo": false,
		"aoColumns" : [ { "sWidth": "600px" }, { "sWidth": "250px" } ],
	});
	$("tfoot select").change(function() {
		/* Filter on the column (the index) of this element */
		gTable.fnFilter("" + this.value, 1);
	});
	
	// All'avvio porte e finestre non sono selezionate
	$('[id^="window_north"], [id^="door_north"]').parents('.field-group').hide();
	$('[id^="window_east"], [id^="door_east"]').parents('.field-group').hide();
	$('[id^="window_south"], [id^="door_south"]').parents('.field-group').hide();
	$('[id^="window_west"], [id^="door_west"]').parents('.field-group').hide();	
});

var construction_dialog_originator = {
	id : "-1",
	name : ""
};
function doUpdateSelection(id, name) {
	construction_dialog_originator.id.attr('value', id);
	construction_dialog_originator.name.attr('value', name);
	$('#X-browse-constructions').dialog('close');
	$('#X-browse-glazings').dialog('close');
}

function getHintForWall(status) {
	var text = "";
	switch (status) {
	case 'adiabatic':
		text = "La parete è trattata come una struttura adiabatica";
		break;
	case 'external':
		text = "La parete è esposta all'ambiente esterno";
		break;
	case 'terrain':
		text = "La parete è adiacente al terreno";
		break;
	case 'windowed':
		text = "La parete contiene porte e/o finestre";
		break;
	default:
		return;
	}

	return text;
}

function getHintForFloor(status) {
	var text = "";
	switch (status) {
	case 'R':
	case 'I':
		text = "Il pavimento è adiacente al piano sottostante";
		break;
	case 'G':
		text = "Il pavimento è adiacente al terreno";
		break;
	default:
		return;
	}

	return text;
}

function getHintForCeiling(status) {
	var text = "";
	switch (status) {
	case 'R':
		text = "Il soffitto è adiacente al tetto";
		break;
	case 'I':
	case 'G':
		text = "Il soffitto è adiacente al piano sovrastante";
		break;
	default:
		return;
	}

	return text;
}

function setWallPanelHints(wall, status) {
	switch (wall) {
	case "wall_north_status":
		$("#wall_north + .hint").text(getHintForWall(status));
		break;
	case "wall_east_status":
		$("#wall_east + .hint").text(getHintForWall(status));
		break;
	case "wall_south_status":
		$("#wall_south + .hint").text(getHintForWall(status));
		break;
	case "wall_west_status":
		$("#wall_west + .hint").text(getHintForWall(status));
		break;
	default:
		break;
	}
}

function setDoorAndWindowPanelVisibility(wall, status) {
	switch (wall) {
	case "wall_north_status":
		if (status == "windowed") {
			$('[id^="window_north"], [id^="door_north"]').parents('.field-group').show();
		} else {
			$('[id^="window_north"], [id^="door_north"]').parents('.field-group').hide();
		}
		break;
	case "wall_east_status":
		if (status == "windowed") {
			$('[id^="window_east"], [id^="door_east"]').parents('.field-group').show();
		} else {
			$('[id^="window_east"], [id^="door_east"]').parents('.field-group').hide();
		}
		break;
	case "wall_south_status":
		if (status == "windowed") {
			$('[id^="window_south"], [id^="door_south"]').parents('.field-group').show();
		} else {
			$('[id^="window_south"], [id^="door_south"]').parents('.field-group').hide();
		}
		break;
	case "wall_west_status":
		if (status == "windowed") {
			$('[id^="window_west"], [id^="door_west"]').parents('.field-group').show();
		} else {
			$('[id^="window_west"], [id^="door_west"]').parents('.field-group').hide();
		}
		break;
	default:
		break;
	}
}